package com.elive.controller.customer;

import com.elive.common.Const;
import com.elive.common.ServerResponse;
import com.elive.pojo.User;
import com.elive.service.IApartmentService;
import com.elive.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by wuyin on 2018/3/26.
 */
@Controller
@RequestMapping("/customer/apartment")
public class ApartmentController {

    @Autowired
    private IApartmentService iApartmentService;

    @RequestMapping(value = "getRecommendApartmentList.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getRecommendApartmentList(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        return iApartmentService.getRecommendApartmentList(pageNum, pageSize);
    }


    @RequestMapping(value = "getApartmentListByCityId.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getApartmentListByCityId(Integer cityId, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        return iApartmentService.getApartmentListByCityId(cityId, pageNum, pageSize);
    }


    @RequestMapping(value = "updateLikeCountByApartmentId.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateLikeCountByApartmentId(String sessionId,Integer apartmentId,HttpSession session,HttpServletRequest request) {
        if(SessionUtil.sessionMap.containsKey(sessionId)){
            session = SessionUtil.sessionMap.get(sessionId);
        }
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return ServerResponse.createByErrorMessage("请先登录再点赞");
        }
        return iApartmentService.updateLikeCountByApartmentId(apartmentId,currentUser.getId());
    }

    @RequestMapping(value = "cancelLikeCountByApartmentId.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse cancelLikeCoundByApartmentId(String sessionId,Integer apartmentId,HttpSession httpSession) {
        if(SessionUtil.sessionMap.containsKey(sessionId)){
            httpSession = SessionUtil.sessionMap.get(sessionId);
        }
        User currentUser = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        return iApartmentService.cancelLikeCountByApartmentId(apartmentId,currentUser.getId());
    }

    @RequestMapping(value = "getApartmentInfo.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getApartmentInfo(Integer apartmentId, HttpSession httpSession) {
        return iApartmentService.getApartmentInfo(apartmentId);
    }

    @RequestMapping(value = "isLiked.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse isLike(String sessionId,Integer apartmentId, HttpSession httpSession){
        if(SessionUtil.sessionMap.containsKey(sessionId)){
            httpSession = SessionUtil.sessionMap.get(sessionId);
        }
        User currentUser = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return ServerResponse.createBySuccess("成功1",false);
        }
        return  iApartmentService.isLiked(apartmentId,currentUser.getId());
    }

    @RequestMapping(value ="getApartmentListByHostId", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getApartmentList(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,Integer hostId){
        return iApartmentService.getApartmentListByHostId(pageNum,pageSize,hostId);
    }


}